//
//  ShoperDetailViewController.m
//  EasyToTesco
//
//  Created by mc on 16/2/22.
//  Copyright © 2016年 Jack. All rights reserved.
//   商品详情页面

#import "ShoperDetailViewController.h"
#import "ShoperDetailOneTableViewCell.h"
#import "ShoperDetailTwoTableViewCell.h"
//订单详情
//#import "OrderDetailViewController.h"
//商品简介
//#import "GoodsIntroViewController.h"
//lunbo
#import "SDCycleScrollView.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "CWStarRateView.h"
#import "NetWorkTools.h"
#import "CreatOrderViewController.h"
#import "JudgeUserLogInTool.h"
#import "SDWebImageManager.h"
#import "UIImageView+WebCache.h"

//确认订单
//#import "ConfirmViewController.h"

//富文本
//#import "RCLabel.h"
//#import "RCViewCell.h"

@interface ShoperDetailViewController ()<UITableViewDelegate, UITableViewDataSource, SDCycleScrollViewDelegate>
{
    //判断是否显示全部评价
    BOOL judgeCell;
    NSString *EvaluationNum;
    UIView *ggView;
    
}
@property (nonatomic, strong)NSMutableDictionary *ShoperDetailDic;

@property (nonatomic, strong)UITableView *shopdetailtableview;
//存放图片的数组
@property (nonatomic, strong)NSMutableArray *picArray;

@property (nonatomic,strong)NSDictionary *xingYingDict;

@end

@implementation ShoperDetailViewController

- (NSDictionary *)xingYingDict {
    if (!_xingYingDict) {
        _xingYingDict = [NSDictionary dictionary];
    }
    return _xingYingDict;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    if (self.xingYingShopID.stringValue.length) {
        [self loadXingYingArrayData];
    } else {
        

    }
    [self ShoperDetailInitSomething];
    [self shoperdetailUI];
    [self ShoperDetailtableviewUI];
    
    [self addlastView];
    
    self.title = @"商品详情";
}

- (void)loadXingYingArrayData {
    NSDictionary *dict = @{
                           @"ProductId":self.xingYingShopID
                           };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"PRODUCTINFOQUERY" completeBlock:^(id object) {
        NSArray *array = [NetWorkTools arrayWithJsonString:[object objectForKey:@"BackData"]];
        self.xingYingDict = array[0];
        
        NSLog(@"%@",self.xingYingDict);
        
        [self.shopdetailtableview reloadData];
    }];
}

- (void)ShoperDetailInitSomething {
    judgeCell = YES;
    EvaluationNum = @"156";
    self.picArray = [@[]mutableCopy];
    self.ShoperDetailDic = [[NSMutableDictionary alloc] initWithCapacity:0];
}

- (void)ShoperDetailtableviewUI{
    self.shopdetailtableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WEITH, HIGHT + 64) style:UITableViewStyleGrouped];
    self.shopdetailtableview.dataSource = self;
    self.shopdetailtableview.delegate = self;
    [self.view addSubview:self.shopdetailtableview];
}

- (void)shoperdetailUI {
//    [self addShoperdetailtopView];
    
    
    [self addlastView];
}


- (void) addlastView {
    UIView *lastView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - (HEIGHT * 0.1 - 20), WEITH, HIGHT*0.1 - 20)];
    [self.view addSubview:lastView];
    lastView.userInteractionEnabled = YES;
    lastView.backgroundColor = [UIColor blackColor];
//    NSArray *array= @[@"加入订单", @"立即购买"];
    
//    for (NSInteger i=0; i<2; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, WEITH, lastView.frame.size.height);
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [button setTitle:@"结算订单" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(lastBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 330;
//        if (i == 1) {
            button.backgroundColor = [UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        }else {
//            button.backgroundColor = [UIColor colorWithRed:244/255. green:244/255. blue:244/255. alpha:1];
//            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        }
//        
    
        [lastView addSubview:button];
//    }
}

- (void)lastBtnClick:(UIButton *)button {
    
    if([JudgeUserLogInTool JudgeUserLonInStatus]) {
        CreatOrderViewController *creat = [[CreatOrderViewController alloc] init];
        
        creat.shopID = [self.xingYingDict objectForKey:@"Id"];
        
        [self.navigationController pushViewController:creat animated:YES];
    } else {
        return;
    }
}



//返回上一级页面


#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 2;
    }else {
        if (judgeCell == YES) {
            return 2;
        } else {
            return 10;
        }
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *identifier = @"ShoperDetailOneTableViewCell";
        ShoperDetailOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"ShoperDetailOneTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        if (self.xingYingShopID.stringValue.length) {
            cell.Titlelabel.text = [NSString stringWithFormat:@"%@",self.xingYingDict[@"Title"]];
            cell.pingpailabel.text = [NSString stringWithFormat:@"品牌"];
            cell.oldPriceLabel.text = [NSString stringWithFormat:@"库存数量:%@",self.xingYingDict[@"SecurityStock"]];
            cell.PriceNewLabel.text = [NSString stringWithFormat:@"会员价:"];
            cell.SCpricelabel.text = [NSString stringWithFormat:@"市场价:%@",[self.xingYingDict objectForKey:@"SellPrice"]];
        } else {
            cell.Titlelabel.text = [NSString stringWithFormat:@"%@", self.ShoperDetailDic[@"title"]];
            cell.pingpailabel.text = [NSString stringWithFormat:@"品牌:%@", self.ShoperDetailDic[@"pinpai"]];
            cell.oldPriceLabel.text = [NSString stringWithFormat:@"库存数量:%@", self.ShoperDetailDic[@"kucun"]];
            cell.PriceNewLabel.text = [NSString stringWithFormat:@"会员价:%@", self.ShoperDetailDic[@"huiyuanjia"]];
            cell.SCpricelabel.text = [NSString stringWithFormat:@"市场价:%@", self.ShoperDetailDic[@"shichangjia"]];
        }
        
        return cell;
        
    } else if (indexPath.section == 1) {

        static NSString *identifier = @"GoodsIntro";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        if (indexPath.row == 0) {
            cell.textLabel.text = @"商品简介";

        } else {
            cell.textLabel.text = @"规格选择";
        }
                return cell;
    } else {
        static NSString *identifier = @"ShoperDetailTwoTableViewCell";
        ShoperDetailTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"ShoperDetailTwoTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.shoperTwoPic.layer.cornerRadius = CGRectGetHeight(cell.shoperTwoPic.frame)/2;
            cell.shoperTwoPic.layer.masksToBounds = YES;
        }
        return cell;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
#pragma mark -- UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 90;
    } else if (indexPath.section == 1) {
    
        return 30;

    } else {
        return 70;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return HIGHT/4;
    } else if (section == 1){
        return 0.01;
    } else {
        return 30;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return 0;
    } else {
        return 10;
    }
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor whiteColor];
    if (section == 0) {
//        if (self.picArray.count!=0) {
            [self addSdcycleView:headerView];
//        }
    } else {
        if (section == 1) {
//            titlabel.text = @"商品简介";
        } else {
            
            UILabel *titlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 200, 20)];
            titlabel.textAlignment = NSTextAlignmentLeft;
            titlabel.font = [UIFont systemFontOfSize:16];
            titlabel.textColor = [UIColor blackColor];
            [headerView addSubview:titlabel];
            titlabel.text = [NSString stringWithFormat:@"商品评价(%@)", EvaluationNum];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(CGRectGetMaxX(titlabel.frame)+10, 5, WEITH-CGRectGetMaxX(titlabel.frame)-10, 20);
            [button setTitle:@"加载更多评价" forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            button.titleLabel.textAlignment = NSTextAlignmentRight;
            [button setTitleColor:[UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(addjudgeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [headerView addSubview:button];
            
        }
        
    }
    return headerView;
}


- (void)addSdcycleView:(UIView *)subview {
    //加载本地图片
    //                        [UIImage imageNamed:@"L2.jpg"],
    //                        [UIImage imageNamed:@"L3.jpg"],
    //                        [UIImage imageNamed:@"L4.jpg"]
    //                        ];
    //加载网络图片
    // 情景二：采用网络图片实现
    
    if (self.xingYingShopID.stringValue.length) {
       
        UIImage *image = [UIImage imageNamed:@"dress"];
        
        NSArray *imagesURL = @[image];
        
        CGFloat w = [UIScreen mainScreen].bounds.size.width;
        SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, w, HIGHT/4) imagesGroup:imagesURL];
        
        // 创建不带标题的图片轮播器
        cycleScrollView.delegate = self;
        [subview addSubview:cycleScrollView];
        // 轮播时间间隔，默认1.0秒，可自定义
        cycleScrollView.autoScrollTimeInterval = 4.0;

    } else {
        NSArray *images = self.picArray;
        
         NSArray *imagesURL = [NSArray arrayWithArray:images];
        
        CGFloat w = [UIScreen mainScreen].bounds.size.width;
        // 创建不带标题的图片轮播器
        SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, w, HIGHT/4) imageURLsGroup:imagesURL];
        cycleScrollView.delegate = self;
        [subview addSubview:cycleScrollView];
        // 轮播时间间隔，默认1.0秒，可自定义
        cycleScrollView.autoScrollTimeInterval = 4.0;

    }
    
    
    
  }
//轮播图的代理方法
#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{

    NSLog(@"---点击了第%ld张图片", index);
}




//判断是否加载更多的评价
- (void)addjudgeBtnClick:(UIButton *)button {
    if (judgeCell == YES) {
        [button setTitle:@"收回更多评价" forState:UIControlStateNormal];
    } else {
        [button setTitle:@"加载更多评价" forState:UIControlStateNormal];
    }
    
    judgeCell = !judgeCell;
    [self.shopdetailtableview reloadData];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footView = [[UIView alloc] init];
    footView.backgroundColor = [UIColor grayColor];
    return footView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
//            GoodsIntroViewController *goodsIntroVC = [[GoodsIntroViewController alloc] init];
//            goodsIntroVC.htmlStr = [NSString stringWithFormat:@"%@", self.ShoperDetailDic[@"about"]];
//            [self presentViewController:goodsIntroVC animated:YES completion:nil];
        } else {
//            [self addggViewUI];
        }
        
    }
}

- (void)addggViewUI {
    UITapGestureRecognizer * recognizer;
    // handleSwipeFrom 是偵測到手势，所要呼叫的方法
    recognizer = [[UITapGestureRecognizer alloc] initWithTarget:nil action:@selector(tapclick)];
    // 不同的 Recognizer 有不同的实体变数
    // 例如 SwipeGesture 可以指定方向
    // 而 TapGesture 則可以指定次數
    
    [self.view addGestureRecognizer:recognizer];
    
    ggView = [[UIView alloc] initWithFrame:CGRectMake(0, ((HIGHT*0.9)/3)*2, WEITH, (HIGHT*0.9)/3)];
    ggView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:ggView];
}

- (void)tapclick {
    [ggView removeFromSuperview];
}


@end
